import { insertApi } from '@/api/url/scanCode';
import { deepCopy, isNullPm } from '@/utils/baseMethods';
import { showNotify, showToast } from '@nutui/nutui';
import { reactive, watch } from 'vue';

export default function importFunction() {
  const base = reactive<any>({
    formData: {
      station: '汽油-越野车',
      frameCode: null,
      barCode: null,
    },
    show: false,
    electricEnergy: ['电动-越野车', '电动-沙滩车', '电动-自行车', '电动-儿童车'],
    gasoline: ['汽油-越野车', '汽油-沙滩车'],
    submitList: [],
  });

  // 汽油 - 越野车
  // 汽油 - 沙滩车
  // 需要绑定车架码、汽油机码
  // 电动-越野车
  // 电动-沙滩车
  // 电动-自行车
  // 电动-儿童车
  // 按图中模式执行
  // 需要绑定车架码-->控制器码、电池码、电机码
  /**
   * 车架码等于唯一ID
   * 需要绑定其他的值
   */
  const readCodeName = () => {
    const electricProcess = ['控制器码', '电池码', '电机码'];
    let label: any = null;
    let placeholder = '请扫';
    if (base.gasoline.includes(base.formData.station)) {
      label = '汽油机码';
      placeholder += label;
    } else if (base.electricEnergy.includes(base.formData.station)) {
      label = electricProcess[base.formData.process];
      placeholder += label;
    }
    return {
      label,
      placeholder,
    };
  };

  const init = () => {
    processRead();
  };

  /**
   * @exports processRead 读取工序
   */
  const processRead = (): any => {
    if (!judgeWhether()) {
      base.formData = {
        station: isNullPm(localStorage.getItem('LABELNAME')) ? '汽油-越野车' : localStorage.getItem('LABELNAME'),
        frameCode: null,
        barCode: null,
      };

      submitListPl();
      return false;
    }

    base.formData = judgeWhether();
    submitListPl();
  };

  /**
   * @exports submitListPl
   */
  const submitListPl = () => {
    base.submitList = [];
    if (base.electricEnergy.includes(base.formData.station)) {
      base.submitList = [
        {
          label: '控制器码',
          value: null,
        },
        {
          label: '电池码',
          value: null,
        },
        {
          label: '电机码',
          value: null,
        },
        {
          label: '前减震-左',
          value: null,
        },
        {
          label: '前减震-右',
          value: null,
        },
        {
          label: '后减震',
          value: null,
        },
      ];
    } else
      base.submitList = [
        {
          label: '汽油机码',
          value: null,
        },
      ];
  };
  /**
   * @exports judgeWhether  判断是否存在 RECORDER
   * @returns
   */
  const judgeWhether = () => {
    const steam: any = localStorage.getItem('RECORDER');
    const steamBom: any = isNullPm(steam) ? false : JSON.parse(steam);
    return steamBom;
  };

  const columns = [
    { text: '汽油-越野车', value: '汽油-越野车' },
    { text: '汽油-沙滩车', value: '汽油-沙滩车' },
    { text: '电动-越野车', value: '电动-越野车' },
    { text: '电动-沙滩车', value: '电动-沙滩车' },
    { text: '电动-自行车', value: '电动-自行车' },
    { text: '电动-儿童车', value: '电动-儿童车' },
  ];

  /**
   * @exports confirm 点击确认
   * @param param0
   */
  const confirm = ({ selectedValue }) => {
    base.formData.station = selectedValue[0];
    if (base.gasoline.includes(base.formData.station)) localStorage.removeItem('RECORDER');
    submitListPl();
    base.show = false;
  };

  const dataUp = (): any => {
    const frameCode = base.formData.frameCode;
    const station = deepCopy(base.formData.station);

    const putList: any = [];
    if (!isNullPm(frameCode)) {
      base.submitList.map((res: any) => {
        const steam = {
          station: res.label,
          frameCode: frameCode,
          barCode: res.value,
        };
        if (!isNullPm(res.value)) putList.push(steam);
      });
    }
    localStorage.setItem('LABELNAME', station);
    base.formData.barCode = null;
    const RECORDER: any = {
      ...deepCopy(base.formData),
      frameCode: null,
    };
    base.formData = {
      station: station,
      frameCode: null,
      barCode: null,
    };
    if (base.electricEnergy.includes(base.formData.station)) localStorage.setItem('RECORDER', JSON.stringify(RECORDER));

    if (putList.length > 0) {
      putList.map((res: any) => {
        insertApi(res).then((res: any): any => {
          init();
          showNotify.success('添加成功');
        });
      });
    }
  };
  /**
   * 监听当数据不一致时,清除code码
   */
  // watch(
  //   () => base.formData.frameCode,
  //   (): any => {
  //     if (!judgeWhether()) return false;
  //     if (base.formData.frameCode !== judgeWhether().frameCode) base.formData.barCode = null;
  //   },
  // );
  return {
    base,
    columns,
    confirm,
    dataUp,

    init,
  };
}
